
#include "DYSvrNetMan.h"
#include "TG_NET_TGA_DEF.h"
#include "stdio.h"


int DYSvrNetMan::TcpAnchorDispatch(SOCKET hClientSocket,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;


	switch( pMsgHead->nMsgType )
	{
	case MSG_TYPE_LOGIN:
		{
			if (pMsgHead->nDataLen   != sizeof(TG_ANCHOR_INFO))
			{
				nRet = TG_ERR_NETWORK_SEND_ERROR ;
				break;
			}

			SvrNetRPSession*  pSession = m_xSessionMan.AddSession(this,(TG_ANCHOR_INFO*)pBuffer);
			if ( pSession == NULL )
			{
				nRet = TG_ERR_FAILURE ;
				break ;
			}
			else
			{
				nRet = pSession->StartRealStream(TG_NET_PROTOCOL_TCP,hClientSocket);
			}
		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
		}
		break;
	}


	pRetMsgHead->nFrameSize = 0;
	pRetMsgHead->nErrFlag   = nRet;
	pRetMsgHead->nDataLen = 0 ;
	pRetMsgHead->SetCRC();

	send(hClientSocket,(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),0);
	
	if(nRet != TG_ERR_SUCCESS)
	{
		close( hClientSocket );
		hClientSocket = NULL;
	}
	return nRet;
}

int DYSvrNetMan::UdpAnchorDispatch(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	char szRetBuffer[256];
	memset(szRetBuffer,0,sizeof(szRetBuffer));

	NET_PACKET_HEAD *pRetMsgHead = (NET_PACKET_HEAD*)szRetBuffer;
	pRetMsgHead->nMsgType = pMsgHead->nMsgType + 1;


// 	switch( pMsgHead->nMsgType )
// 	{
// 	case MSG_TYPE_ANC_LOGIN:
// 		{
// 		}
// 		break;
// 	default:
// 		{
// 			nRet = UdpAnchorCommand(hSocket,pAddr,pMsgHead,pBuffer);
// 		}
// 		break;
// 	}
// 
// 	if( TG_ERR_SUCCESS != nRet )
// 	{
// 		pRetMsgHead->nFrameSize = 0;
// 		pRetMsgHead->nErrFlag   = nRet;
// 
// 		sendto( hSocket,
// 			(char*)szRetBuffer,sizeof(NET_PACKET_HEAD),
// 			0,
// 			(struct sockaddr*)&pAddr->xAddr,pAddr->nAddrLen );
// 	}

	return nRet;
}

int DYSvrNetMan::UdpAnchorCommand(SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	int nRet = TG_ERR_SUCCESS;

	switch( pMsgHead->nMsgType )
	{
	case 1:
		{

		}
		break;
	default:
		{
			nRet = TG_ERR_FAILURE;
			break;
		}
	}

	return nRet;
}